package com.neuedu.shop.controller;


import com.neuedu.shop.entity.Admin;
import com.neuedu.shop.service.AdminService;
import com.neuedu.shop.util.CommonUtil;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Qin
 * @since 2021-04-10
 */
@Controller
@RequestMapping("/admin")
public class AdminController {
    //注入
    @Resource
    private AdminService service;
    @RequestMapping(value = "/admins",method = RequestMethod.GET)
    public String selectAll(ModelMap map){
        //转发
        map.addAttribute("admins",service.selectList(null));
        return "admins.jsp";
    }
    @RequestMapping(value = "/insertAdmin",method = RequestMethod.POST)
    public String insertAdmin(Admin admin){
        /*service.insert(admin);*/

        service.insertAdmin(admin);
        return "redirect:admins";
    }
    @RequestMapping(value = "findById",method = RequestMethod.GET)
    public String findById(Integer id,ModelMap map){
        map.addAttribute("admin",service.selectById(id));
        return "forward:update_admins.jsp";
    }
    @RequestMapping(value = "/getOriginPwd",method = RequestMethod.GET,produces = "text/plain;charset=UTF-8")
    @ResponseBody
    //SpringMVC 处理Ajax 返回一个JSON对象
    public String getOriginPwd(Integer id,String pwd){
System.out.println(id + "," + pwd);
        Admin admin = service.selectById(id);
        List<String> strs = new ArrayList<>();
        strs.add("原始密码验证成功!");
        strs.add("原始密码验证失败!");
            if(admin.getAdminPassword().equals(CommonUtil.stringToMD5(pwd))){
                return strs.get(0);
            }else{
                return strs.get(1);
            }
    }
    @RequestMapping(value = "/updateAdmin",method = RequestMethod.POST)
    public String updateAdmin(Admin admin){
System.out.println(admin);
        service.updateAdmin(admin);
       return "redirect:admins";
    }
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public String delete(Long id){
System.out.println(id);
        service.deleteById(id);
        return "redirect:admins";
    }
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(Admin admin,String code, HttpSession session, HttpServletRequest request){
        String path = request.getServletContext().getContextPath();

        Admin adminDB = service.login(admin.getAdminName());
        if(adminDB != null && adminDB.getAdminPassword().equals(admin.getAdminPassword())){
            String number = (String)session.getAttribute("number");
            System.out.println("从session中获取："+number);
            System.out.println("前台传入："+admin+",用户输入："+code);
            if(number.equals(code)){
                //登录成功
                session.setAttribute("admin",adminDB);
                return "redirect:/index.jsp";
            }
            //如果验证码不相等
            else{
                request.setAttribute("code_error","验证码错误！");
                return "login.jsp";
            }
        }else {
            //登陆失败
            if (adminDB == null){
                request.setAttribute("login_error1","用户名不存在");
            }else if (!adminDB.getAdminPassword().equals(admin.getAdminPassword())){
                request.setAttribute("login_error2","用户名或密码错误");
            }
            return "login.jsp";
        }

    }
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        //注销功能
        session.invalidate();
        return "redirect:login.jsp";
    }
}

